package belf.migrate.plugin.mysql;

import belf.migrate.api.table.schema.CatalogTable;
import belf.migrate.api.table.schema.ColumnMetaData;
import belf.migrate.api.table.schema.TablePath;
import belf.migrate.api.table.schema.TableSchema;
import belf.migrate.api.util.CatalogUtils;
import belf.migrate.api.util.DatabaseType;
import com.mysql.cj.MysqlType;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

import static belf.migrate.api.common.Preconditions.checkNotNull;

public class MysqlCreateTableSqlBuilder {

    private final String tableName;

    private TableSchema tableSchema;

    private String comment;

    private String engine;
    private String charset;
    private String collate;

    private String fieldCase;

    private MysqlCreateTableSqlBuilder(String tableName) {
        checkNotNull(tableName, "tableName must not be null");
        this.tableName = tableName;
    }

    public static MysqlCreateTableSqlBuilder builder(
            TablePath tablePath, CatalogTable catalogTable) {
        checkNotNull(tablePath, "tablePath must not be null");
        checkNotNull(catalogTable, "catalogTable must not be null");

        TableSchema tableSchema = catalogTable.getTableSchema();
        checkNotNull(tableSchema, "tableSchema must not be null");

        return new MysqlCreateTableSqlBuilder(tablePath.getTableName())
                .comment(catalogTable.getComment())
                // todo: set charset and collate
                .engine(null)
                .charset(null)
                .tableSchema(tableSchema)
                .fieldCase(catalogTable.getOptions().get("fieldCase"));
    }

    public MysqlCreateTableSqlBuilder tableSchema(TableSchema tableSchema) {
        this.tableSchema = tableSchema;
        return this;
    }

    public MysqlCreateTableSqlBuilder fieldCase(String fieldCase) {
        this.fieldCase = fieldCase;
        return this;
    }

    public MysqlCreateTableSqlBuilder engine(String engine) {
        this.engine = engine;
        return this;
    }

    public MysqlCreateTableSqlBuilder charset(String charset) {
        this.charset = charset;
        return this;
    }

    public MysqlCreateTableSqlBuilder collate(String collate) {
        this.collate = collate;
        return this;
    }

    public MysqlCreateTableSqlBuilder comment(String comment) {
        this.comment = comment;
        return this;
    }

    public String build(String catalogName) {
        List<String> sqls = new ArrayList<>();
        sqls.add(
                String.format(
                        "CREATE TABLE %s (\n%s\n)",
                        CatalogUtils.quoteIdentifier(tableName, fieldCase, DatabaseType.MYSQL.getQuoteType().getValue(), DatabaseType.MYSQL.getQualifiedType()),
                        buildColumnsIdentifySql(catalogName)));
        if (engine != null) {
            sqls.add("ENGINE = " + engine);
        }
        if (charset != null) {
            sqls.add("DEFAULT CHARSET = " + charset);
        }
        if (collate != null) {
            sqls.add("COLLATE = " + collate);
        }
        if (comment != null) {
            sqls.add("COMMENT = '" + comment + "'");
        }
        return String.join(" ", sqls) + ";";
    }

    private String buildColumnsIdentifySql(String catalogName) {

        return null;
    }

    private String buildColumnIdentifySql(ColumnMetaData column) {
        return null;
    }

    private String buildPrimaryKeySql() {
        return null;
    }

}
